use log::info;
use reqwest::{Client, Proxy};

pub struct HttpClientBuilder;

impl HttpClientBuilder {
    /// 构建带代理的HTTP客户端
    /// 优先级: 账号级代理 > 任务级代理 > 直连
    pub fn build_with_proxy(
        task_proxy: Option<&String>,
        account_proxy: Option<&String>,
    ) -> Result<Client, String> {
        let mut client_builder = Client::builder();

        // 优先使用账号级代理,其次任务级代理
        let proxy_url = account_proxy.or(task_proxy);

        if let Some(url) = proxy_url {
            info!("使用代理: {}", url);
            let proxy = Proxy::all(url)
                .map_err(|e| format!("代理URL无效: {}", e))?;
            client_builder = client_builder.proxy(proxy);
        } else {
            info!("未配置代理,使用直连");
        }

        client_builder
            .build()
            .map_err(|e| format!("构建HTTP客户端失败: {}", e))
    }
}
